//网页搜索类
#pragma once

#include "PageLRUCach.h"

#include <string>
#include <vector>
#include <set>
#include <memory>
#include <map>
#include <tuple>


#include <cppjieba/Jieba.hpp>
#include <utfcpp/utf8.h>

using std::tuple;
using std::vector;
using std::string;
using std::set;
using std::unique_ptr;
using std::map;


const string load_ch_stop_words_file="../../../data/stopwords/cn_stopwords.txt";
const string load_inverted_index_filename="../../../data/inverted_index.dat";
const string load_offsets_filename="../../../data/offsets.dat";
const string load_pagelib_filename="../../../data/pages.dat";

const int feature=10;

// 存储关键词及其权重的结构


struct SearchResult {
    int id;
    string title;
    string link;
    string abstract; 
};

class WebpageSearch
{
public:
    //进行构造单例
    static WebpageSearch &getWebpageSearch();
    string search(const string &content,
                 const string &page_file_=load_pagelib_filename);
    
private:
    WebpageSearch();
    ~WebpageSearch();
    void wordSegmentation(const string &content); //对content进行分词
    string makeBaseVector(const string &content,const string &filename);//对content进行向量化
    void load_ch_stop_words(const std::string& filename=load_ch_stop_words_file); //加载中文停用词
    void load_offsets_file(const std::string &filename=load_offsets_filename);//加载偏移量文件
    void load_Inverted_file(const std::string &filename=load_inverted_index_filename);//添加倒排索引库
    vector<tuple<int, double, vector<KeywordInfo>>> rankDocumentsByCosineSimilarity(); //根据余弦相似度对文档进行排序
    std::string safeUtf8Substr(const std::string& str, size_t max_chars);
private:
    cppjieba::Jieba m_tokenizer;
    int m_sum;                               //当前文档的词语总数
    int m_documents_size; //文档数量
    unique_ptr<map<string,int>> m_BaseWords; //关键字
    unique_ptr<std::set<std::string>> m_chStopWords;//停用词
    unique_ptr<std::map<std::string,double>> m_BaseVector; //中文词向量
    unique_ptr<map<int,std::pair<int,int>>> m_offsets; //偏移量
    unique_ptr<map<string,vector<std::pair<int,double>>>> m_pageLib; //索引库
    
};